﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Project
{
    public partial class Form1 : Form
    {
        int step = 1;
        int test = 1;
        int num_question = 0;
        int num_click = 0;
        System.IO.StreamWriter sw = new System.IO.StreamWriter("Вывод.txt");
       
        public Form1()
        {
            InitializeComponent();
            ShowDate();
            ShowPdf();
        }

        // обработчик события кнопки "дальше"
        private void button1_Click(object sender, EventArgs e)
        {
            axFoxitReaderOCX1.Dispose();
           // webBrowser1.Visible = false;
            switch (step)
            {
                case 1: this.checkBox1.Checked = true;
                    InitTestWindow();
                    InitStatLabel();
                    this.checkBox2.BackColor = Color.Aqua;
                    this.checkBox1.BackColor = SystemColors.Control;
                    this.button1.Enabled = false;
                    InitBD();
                    step++;
                    break;
                case 3: this.checkBox3.Checked = true;

                    this.checkBox4.BackColor = Color.Aqua;
                    this.checkBox3.BackColor = SystemColors.Control;

                    step++;
                    break;
                case 4: this.checkBox4.Checked = true;

                    this.checkBox5.BackColor = Color.Aqua;
                    this.checkBox4.BackColor = SystemColors.Control;

                    step++;
                    break;
                case 5: this.checkBox5.Checked = true;

                    this.checkBox6.BackColor = Color.Aqua;
                    this.checkBox5.BackColor = SystemColors.Control;

                    this.panel3.Controls.Add(this.Vivod_ok);
                    this.panel3.Controls.Add(this.Vivod);
                    
                    step++;
                    break;

                case 8: button1.Text = "asdfas"; break;
                
                default: break;
            }
        }

        // обработчик соытия кнопки "ответить" для теста 
        private void button2_Click(object sender, EventArgs e)
        {
            int true_flag = 0;
            num_click++;
            if (GiveAns.Text == "ОТВЕТИТЬ")
            {
                for (int i = 0; i < MasAnswer.Length; i++)
                {
                    if (MasAnswer[i].Checked == true && i == AnsRight)
                    {
                        true_flag = 1;
                        break;
                    }
                }
                if (true_flag == 1)
                {           
                    this.StatLabel[num_question].BackColor = System.Drawing.Color.GreenYellow;
                    this.StatLabel[num_question].ForeColor = System.Drawing.Color.Black;
                }
                else
                {
                    TrueAns.Text = "Неправильно";
                    for (int i = 0; i < MasAnswer.Length; i++)
                    {
                        if (MasAnswer[i].Checked == true)
                        {
                            MasAnswer[i].ForeColor = System.Drawing.Color.Red;
                            break;
                        }
                    }        
                    MasAnswer[AnsRight].ForeColor = System.Drawing.Color.Green;
                    this.StatLabel[num_question].BackColor = System.Drawing.Color.Red;
                    this.StatLabel[num_question].ForeColor = System.Drawing.Color.Black;
                }
                TrueAns.Visible = true;
                GiveAns.Text = "ДАЛЕЕ";
            }

            if(GiveAns.Text == "ДАЛЕЕ" && num_click % 2 == 0)
            {
                TrueAns.Visible = false;
                TrueAns.Text = "Правильно";
                GiveAns.Text = "ОТВЕТИТЬ";
                GiveAns.Enabled = false;
                for (int i = 0; i < MasAnswer.Length; i++)
                {
                        MasAnswer[i].Checked = false;
                        MasAnswer[i].ForeColor = System.Drawing.Color.Black;
                }

    
                num_question++;
                if (num_question != 15)
                {
                    LoadQuestion(num_question);
                    this.StatLabel[num_question].BackColor = System.Drawing.Color.Blue;
                    this.StatLabel[num_question].ForeColor = System.Drawing.Color.White;
                }
                else
                {

                    for (int i = 0; i < 15; i++)
                    {
                        StatLabel[i].Dispose();
                    }

                    if (test == 1)
                    {
                        this.checkBox2.Checked = true;
                        this.checkBox3.BackColor = Color.Aqua;
                        this.checkBox2.BackColor = SystemColors.Control;


                        for (int i = 0; i < 4; i++)
                        {
                            MasAnswer[i].Visible = false;
                        }
                        TrueAns.Visible = Question.Visible = GiveAns.Visible = TestPicture.Visible = false;
                    }
                    else
                    {

                        for (int i = 0; i < 4; i++)
                        {
                            MasAnswer[i].Dispose();
                        }
                        TrueAns.Dispose();
                        Question.Dispose();
                        GiveAns.Dispose();
                        TestPicture.Dispose();
                    }

                    this.button1.Enabled = true;

                    step++;
                    test++;

                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            time.Text = DateTime.Now.ToString();
        }

        // событие кнопки "ок" для вывода
        private void Vivod_ok_Click(object sender, EventArgs e)
        {
            sw.WriteLine(Vivod.Text);
            sw.Close();
            Vivod.Dispose();
            Vivod_ok.Dispose();
            num_question = 0;

            this.checkBox6.Checked = true;
            this.checkBox6.BackColor = SystemColors.Control;

            InitStatLabel();
            panel3.Enabled = true;
            for (int i = 0; i < 4; i++)
            {
                MasAnswer[i].Visible = true;
            }
            Question.Visible = GiveAns.Visible = TestPicture.Visible = true;
            this.button1.Enabled = false;            
            step++;
        }

        private void MasAnswer_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < MasAnswer.Length; i++)
            {
                if (MasAnswer[i].Checked == true)
                {
                    GiveAns.Enabled = true;
                    break;
                }
            }
        }
    }
}
